package java基础知识;

/**
 * @author coderFu
 * @create 2022/3/4
 * 删除String中指定字符的11种方法
 */
public class DeleteStringAllFunction {

    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();

        // 第一种通过 转换为 单个字符来处理

    }

    /**
     * 遍历， 通过转换为字符数组来处理
     * @param sourceString
     * @param chElemData
     * @return
     */
    public String deleteCharString1(String sourceString, char chElemData){

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < sourceString.length(); i++) {
            if (sourceString.charAt(i) != chElemData){
                sb.append(sourceString.charAt(i));
            }
        }
        return sb.toString();
    }


    /**
     * 方法二： 通过截取的方式处理
     * @param sourceString
     * @param chElemData
     * @return
     */
    public String deleteCharString2(String sourceString, char chElemData){

        StringBuilder sb = new StringBuilder("");
        int indexId = 0;

        for (int i = 0; i < sourceString.length(); i++) {

            if (sourceString.charAt(i) == chElemData){
                if ( indexId > 0){
                    sb.append(sourceString.substring(indexId,i));
                }else {
                    indexId++;
                }
            }
        }
        // 把后续的没有断点的部分加进去
        if (indexId <= sourceString.length()){
            sb.append(sourceString.substring(indexId,sourceString.length()));
        }

        return sb.toString();
    }


    /**
     * 将字符串变为可读序列，通过String的方法进行替换
     * @param sourceString
     * @param chElemData
     * @return
     */
    public String deleteCharString6(String sourceString, char chElemData){
        String tmpString = "";

        tmpString += chElemData;
        tmpString.subSequence(0,0);

        String deleteString = "";
        // 用 后面的元素，替换掉前面的元素 ！！
        deleteString = sourceString.replace(tmpString,deleteString.subSequence(0,0));
        return deleteString;
    }

    /**
     * 直接找到要替换的元素，然后直接替换
     * @param sourceString
     * @param chElemData
     * @return
     */
    public String deleteCharString7(String sourceString, char chElemData){

        String temString  = "";
        temString += chElemData;

        StringBuffer stringBuffer = new StringBuffer(sourceString);
        int iFlag = -1;

        do {
            iFlag = stringBuffer.indexOf(temString);
            if (iFlag != -1){
                stringBuffer = stringBuffer.replace(iFlag, iFlag + 1, "");
            }
        }while (iFlag != -1);
        return stringBuffer.toString();
    }


    public String deleteCharString8(String sourceString, char chElemData){

        String temString  = "";
        temString += chElemData;

        StringBuffer stringBuffer = new StringBuffer(sourceString);
        int iFlag = -1;

        do {
            iFlag = stringBuffer.indexOf(temString);
            if (iFlag != -1){
                // 直接调用删除方法，直接删除！
                stringBuffer.deleteCharAt(iFlag);
            }
        }while (iFlag != -1);
        return stringBuffer.toString();
    }
}
